﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace a25_BitmapDecoder
{
    class App : Application
    {
        [STAThread]
        public static void Main()
        {
            var app = new App();
            app.Run();
        }


        private Window mainWindow;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            CreateMainWindow();
        }

        private void CreateMainWindow()
        {
            mainWindow = new Window { Title = "a25_BitmapDecoder", Height= 400, Width=800};

            //使用stream方式打开image file.
            var stream = new FileStream("shuihu.jpg", FileMode.Open, FileAccess.Read, FileShare.Read);
            var decoder = new JpegBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.Default);
            BitmapSource bitmapSource = decoder.Frames[0];

            var image1 = new Image
            {
                Source = bitmapSource,
                Stretch = Stretch.None,
                Margin = new Thickness(10)
            };

            //使用uri方式打开image file.
            var uri = new Uri("shuihu.jpg", UriKind.Relative);
            decoder = new JpegBitmapDecoder(uri, BitmapCreateOptions.PreservePixelFormat,
                BitmapCacheOption.Default);
            bitmapSource = decoder.Frames[0];

            var image2 = new Image
            {
                Source = bitmapSource,
                Stretch = Stretch.None,
                Margin = new Thickness(10)
            };



            var mySp = new StackPanel
            {
                Orientation = Orientation.Vertical,
                VerticalAlignment = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };
            
            TextBlock tb = new TextBlock { Text = "Load image by uri" };
            mySp.Children.Add(tb);
            mySp.Children.Add(image1);
            
            tb = new TextBlock { Text = "Load image by stream" };
            mySp.Children.Add(tb);
            mySp.Children.Add(image2);


            var mySc = new ScrollViewer();
            mySc.Content = mySp;
            mainWindow.Content = mySc;
            mainWindow.Show();

        }


    }
}
